package com.csust.travel.controller;

import com.csust.travel.entity.pageDate;
import com.csust.travel.entity.ResBean;
import com.csust.travel.entity.Sellers;
import com.csust.travel.mapper.SellersMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/sellers")
public class SellersController {
    @Autowired
    SellersMapper sellersMapper;


    @RequestMapping("/all")
    public ResBean findAll(){
        List<Sellers> items = sellersMapper.findAllItem();
        return new ResBean(true,null,items);
    }
    @RequestMapping("/del")
    public  ResBean delItem(Integer id){
        try {
            sellersMapper.delItem(id);
            return new ResBean(true,"删除成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false,"删除失败");
    }
    @RequestMapping("/create")
    public ResBean saveItem(@RequestBody Sellers item) {
        try {
            sellersMapper.saveItem(item);
            return new ResBean(true,"新增卖家成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false,"新增卖家失败666");
    }
    @RequestMapping("/one")
    public ResBean findOne(Integer id){
        Sellers item  = sellersMapper.findOne(id);
        try {
            return new ResBean(true,null,item);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResBean(false,null);
        }
    }


    @RequestMapping("/update")
    public ResBean editItem(@RequestBody Sellers item) {
        try {
            sellersMapper.editItem(item);
            return new ResBean(true,"编辑成功666");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResBean(false,"编辑失败666");
    }

    @RequestMapping("/page")
    public ResBean page(@RequestBody pageDate pagination){
        PageHelper.startPage(pagination.getCurrentPage(),pagination.getPageSize());
        List<Sellers> Items;
        if (pagination.getQueryString() == null || pagination.getQueryString().trim().equals("")){
            Items = sellersMapper.findAllItem();
        }else {
            Items = sellersMapper.findByString(pagination.getQueryString());
        }

        PageInfo<Sellers> sellersPageInfo = new PageInfo<>(Items);

        return new ResBean(true,null,sellersPageInfo);
    }

}
